package com.common.platform.base.config.mail;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;

@Component
@Slf4j
public class MailUtil {

    @Autowired
    Mail mail;


    public String createHtml(String content) {
        String html = "<!doctype html>\n" +
                "<html>\n" +
                "<head>\n" +
                "  <meta charset=\"utf-8\">\n" +
                "  <title>邮件通知</title>\n" +
                "  <style>\n" +
                "    .container {\n" +
                "      width: 60%;\n" +
                "      margin: 10% auto 0;\n" +
                "      background-color: #f0f0f0;\n" +
                "      padding: 2% 5%;\n" +
                "      border-radius: 10px\n" +
                "    }\n" +
                "\n" +
                "    ul {\n" +
                "      padding-left: 20px;\n" +
                "    }\n" +
                "\n" +
                "    ul li {\n" +
                "      line-height: 2.3\n" +
                "    }\n" +
                "\n" +
                "    a {\n" +
                "      color: #20a53a\n" +
                "    }\n" +
                "  </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<div class=\"container\">\n" +
                "  <h1>" + content + "</h1>\n" +
                "  <h3>这是默认index.html，本页面由系统自动生成</h3>\n" +
                "</div>\n" +
                "</body>\n" +
                "</html>";
        return html;
    }

    public void send(String subject, String message) {
//        Mail mail = new Mail();
//        mail.setFlag(true);
//        mail.setHost("smtp.qiye.aliyun.com");
////        mail.setHost("smtp.qq.com");
//        mail.setPort(25);
////        mail.setFrom("painterxiii@foxmail.com");
//        mail.setFrom("m@yonrd.com");
//        mail.setPass("User12345");
////        mail.setPass("gvsyuibivhhdbigb");
//        mail.setNickname("雍正");
//        mail.setReceivers(Collections.singleton("446150793@qq.com"));

        if (mail.isFlag()) {
            HtmlEmail email = new HtmlEmail();
            try {
                email.setHostName(mail.getHost());
                email.setCharset("UTF-8");
                for (String str : mail.getReceivers()) {
                    email.addTo(str);
                }
                email.setFrom(mail.getFrom(), mail.getNickname());
                email.setSmtpPort(mail.getPort());
                email.setAuthentication(mail.getFrom(), mail.getPass());
                email.setSubject(subject);
                email.setMsg(createHtml(message));
                email.send();
                log.info("{} 发送邮件到 {}", mail.getFrom(), StringUtils.join(mail.getReceivers(), ","));
            } catch (EmailException e) {
                log.error(mail.getFrom() + "发送邮件到" + StringUtils.join(mail.getReceivers(), ",") + "失败", e);
            }
        }
    }

    public static void main(String[] args) {
        MailUtil mailUtil = new MailUtil();
        mailUtil.send("hello", "hello");
    }
}

